package com.kl.controller;


import com.kl.domain.CouponDTO;
import com.kl.domain.UserCouponDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Coupon;
import com.kl.entity.TbTaxpayer;
import com.kl.entity.UserCoupon;
import com.kl.service.ICouponService;
import com.kl.service.IUserCouponService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("couponController")
public class CouponController {

    @Autowired
    private ICouponService couponService;
    @Autowired
    private IUserCouponService userCouponService;

    //优惠券信息查询接口
    @ApiOperation(value = "优惠券信息查询接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户Id",required = true,paramType = "query")
    })
    @GetMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result<List<CouponDTO>> selectByCouponId(@RequestParam String userId){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        List<CouponDTO> contactsList = new ArrayList<>();
        try {
            //pageList = userMapper.selectPage(page, null);
            List<Integer> list = userCouponService.selectByUserId(userId);
            for (int i = 0; i < list.size(); i++) {
                Coupon coupon = couponService.getById(list.get(i));
                CouponDTO couponDTO = new CouponDTO();
                BeanUtils.copyProperties(coupon,couponDTO);
                contactsList.add(couponDTO);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(contactsList);
    }

    //优惠券主键id查询接口
    @ApiOperation(value = "优惠券主键id查询接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "id",value = "主键Id",required = true,paramType = "query")
    })
    @GetMapping("selectId")// @ResponseBody  将后台的数据以json的格式返回前台
    public Result<CouponDTO> selectId(@RequestParam String id){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        CouponDTO couponDTO = new CouponDTO();
        try {
            //pageList = userMapper.selectPage(page, null);
            Coupon coupon = couponService.getById(id);
            BeanUtils.copyProperties(coupon,couponDTO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(couponDTO);
    }

    //优惠券信息增加接口
    @ApiOperation(value = "优惠券信息增加接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户Id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "cid",value = "优惠劵Id",required = true,paramType = "query")
    })
    @PostMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result insertById(@RequestParam Long userId,@RequestParam Long cid){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            Coupon coupon = couponService.getById(cid);
            CouponDTO couponDTO = new CouponDTO();
            BeanUtils.copyProperties(coupon,couponDTO);
            if (couponDTO.getCouponCount() <= 0){
                return Result.build(ConstantEnum.ERROR).setResult("优惠券已被抢光");
            }else {
                UserCouponDTO userCouponDTO = new UserCouponDTO();
                userCouponDTO.setUserId(userId);
                userCouponDTO.setCouponId(cid);
                UserCoupon userCoupon = new UserCoupon();
                BeanUtils.copyProperties(userCouponDTO,userCoupon);
                userCouponService.save(userCoupon);
                coupon.setCouponCount(coupon.getCouponCount() - 1);
                couponService.updateById(coupon);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //优惠券信息删除接口
    @ApiOperation(value = "优惠券信息删除接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "cid",value = "优惠劵Id",required = true,paramType = "query")
    })
    @DeleteMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result deleteByCouponId(@RequestParam Long cid){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            couponService.removeById(cid);
            userCouponService.deleteByCid(cid);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
